package com.tjp.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.mysql.cj.util.StringUtils;
import com.tjp.subject.application.convert.SubjectCategoryBOConverter;
import com.tjp.subject.application.convert.SubjectLabelBOConverter;
import com.tjp.subject.application.entity.SubjectLabelDTO;
import com.tjp.subject.common.entity.JZResult;
import com.tjp.subject.domain.convert.SubjectLabelConverter;
import com.tjp.subject.domain.entity.SubjectCategoryBO;
import com.tjp.subject.domain.entity.SubjectLabelBO;
import com.tjp.subject.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: jz-club
 * @description: 题目分类控制器
 * @author: aaronTang
 * @create: 2024-02-22
 */
@RestController
@RequestMapping("/subject/label")
@Slf4j
public class SubjectLabelController {

    @Autowired
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("/add")
    public JZResult<Boolean> insert(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.insert.SubjectLabelDTO is {}", JSON.toJSONString(subjectLabelDTO));
            }

            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");
            // 确保 categoryName 不为空
            Preconditions.checkArgument(!StringUtils.isNullOrEmpty(subjectLabelDTO.getLabelName()), "标签名称不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelBOConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);

            subjectLabelDomainService.insert(subjectLabelBO);
            return JZResult.ok();
        }catch (Exception e) {
            log.error("SubjectCategoryController.insert.error: {}", e.getMessage(), e);
            return JZResult.fail();
        }
    }

    /**
     * 更新标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public JZResult<Boolean> update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.update.subjectLabelDTO is {}", JSON.toJSONString(subjectLabelDTO));
            }

            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelBOConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);


            int result = subjectLabelDomainService.update(subjectLabelBO);

            return JZResult.ok(result);
        }catch (Exception e) {
            log.error("SubjectCategoryController.update.error: {}", e.getMessage(), e);
            return JZResult.fail("更新分类失败");
        }
    }

    /**
     * 删除标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public JZResult<Boolean> delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.delete.subjectLabelDTO is {}", JSON.toJSONString(subjectLabelDTO));
            }

            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelBOConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);


            int result = subjectLabelDomainService.delete(subjectLabelBO);

            return JZResult.ok(result);
        }catch (Exception e) {
            log.error("SubjectCategoryController.delete.error: {}", e.getMessage(), e);
            return JZResult.fail("更新分类失败");
        }
    }

    /**
     * 查询分类下标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryLabelByCategoryId")
    public JZResult<List<SubjectLabelDTO>> queryLabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.SubjectCategoryDTO is {}",
                        JSON.toJSONString(subjectLabelDTO));
            }

            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空");

            SubjectLabelBO subjectLabelBO = SubjectLabelBOConverter.INSTANCE.convertDTOToLabelBO(subjectLabelDTO);

            List<SubjectLabelBO> subjectCategoryBOList = subjectLabelDomainService.quertyLabelByCategoryId(subjectLabelBO);

            List<SubjectLabelDTO> subjectLabelDTOS = SubjectLabelBOConverter.INSTANCE.convertBOListToDTOList(subjectCategoryBOList);
            if (log.isInfoEnabled()) {
                log.info("SubjectLabelController.queryLabelByCategoryId.DTOList is {}", JSON.toJSONString(subjectLabelDTOS));
            }
            return JZResult.ok(subjectLabelDTOS);
        }catch (Exception e) {
            log.error("SubjectLabelController.queryLabelByCategoryId.error: {}", e.getMessage(), e);
            return JZResult.fail("查询分类下标签失败");
        }
    }





}
